<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-编辑${name_china}</title>
<%@include file="/common/head.jsp" %>

</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>编辑${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="form" method="post">
    <div class="form-wrapper">
	  <div class="h4 distance-double">
        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
        <span class="h4-border"></span>
      </div>
      
      <div id="addNew">
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>红包名</p>
	        <input type="text" id="name" name="name" class="summer-input distance-right"  /> 
	        <input type="hidden" id="coupons" name="coupons" class="summer-input summer-validatebox"  data-validatebox="validType:['required','maxLength[50]']" /> 
	        <input type="hidden" id="id" name="id"/>
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>领取次数</p>
	        <input type="text" id="time" name="time" class="summer-input distance-right" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	      <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>金额</p>
	        <input type="text" id="amount" name="amount" class="summer-input" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>使用条件</p>
	        <input type="text" id="useCond" name="useCond" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>起始时间</p>
	        <input type="text" id="startTime" name="startTime" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>结束时间</p>
	        <input type="text" id="endTime" name="endTime" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>图片路径</p>
	        <input type="hidden" id="imagePath" name="imagePath" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>共有标识</p>
	        <input type="text" id="publicSign" name="publicSign" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>数量</p>
	        <input type="text" id="count" name="count" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title"><span class="field-required">*</span>类型</p>
	        <input type="text" id="type" name="type" class="summer-input summer-validatebox" data-validatebox="validType:['required','maxLength[50]']"/> 
	     </div>
	      
	    </div>
     <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
</form>
<script>
    
  var   editData,
  		voData,
        permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {
    	editData = ${editData};
    	voData = ${voData};
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:edit']
        }));
    	
	       //下拉初始化
	   	    $('#type').combobox({
	   	    	async:false,
	   	        defaultValue: editData.type ,
	            valueField : 'code',
	            textField : 'name',
	            url : COMMON_URL.getCode + '?type=T_DM_T_PROJECT_COUPON_TYPE'
	        }); 
	   	  
     	 $('#form').form({
        	onLoadSuccess:function(data){
               
           }
        }).form('loadDataEdit', {
            data: editData
        });
     	//保存按钮
     	$('#funcbar').on('click', 'a[data-permit="${name_english}:edit"]', submit);
     	
     	//返回按钮
     	$('#backBtn, #goBack').on('click',backMain);
  		var html = "";
     	//循环遍历其他组内数据
     	/*  console.log(voData.length);
     	alert(voData.length);  */
     	if(voData == ""){
     		alert("1");
     	}else{
    		 for(var j=0;j<voData.length;j++){
           	html = '<hr height:2px noshade=true/>'
           		+'<div class="form-cont">'
       	        +'<p class="form-cont-title"><span class="field-required">*</span>红包名</p>'
       	        +'<input type="text" id="name'+j+'" name="name'+j+'" value="'+voData[j].name+'" class="summer-input summer-validatebox" data-validatebox="validType:[\'required\',\'maxLength[50]\']" /> '
       	        +'<input type="hidden" id="id" name="id"/>'
       	        +'</div>'
       	     
       	     	+'<div class="form-cont">'
       	        +'<p class="form-cont-title"><span class="field-required">*</span>领取次数</p>'
       	        +'<input type="text" id="time'+j+'" name="time'+j+'" value="'+voData[j].time+'" class="summer-input summer-validatebox" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/> '
       	        +'</div>'
       	     
       	      	+'<div class="form-cont">'
       	        +'<p class="form-cont-title"><span class="field-required">*</span>金额</p>'
       	        +'<input type="text" id="amount'+j+'" name="amount'+j+'" value="'+voData[j].amount+'" class="summer-input" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/>'
       	        +'</div>'
       	     
       	     	+'<div class="form-cont">'
       	        +'<p class="form-cont-title"><span class="field-required">*</span>使用条件</p>'
       	        +'<input type="text" id="useCond'+j+'" name="useCond'+j+'" value="'+voData[j].useCond+'" class="summer-input summer-validatebox" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/>' 
       	        +'</div>'
       	     
       	     	+'<div class="form-cont">'
       	        +'<p class="form-cont-title"><span class="field-required">*</span>起始时间</p>'
       	        +'<input type="text" id="startTime'+j+'" name="startTime'+j+'" value="'+voData[j].startTime+'" class="summer-input summer-validatebox"  onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/> '
       	     	+'</div>'
       	     
       	        +'<div class="form-cont">'
       	        +'<p class="form-cont-title"><span class="field-required">*</span>结束时间</p>'
       	        +'<input type="text" id="endTime'+j+'" name="endTime'+j+'" value="'+voData[j].endTime+'" class="summer-input summer-validatebox" onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\'})" data-validatebox="validType:[\'required\',\'maxLength[50]\']"/>' 
       	     	+'</div>'
       	     
       		     +'<div class="form-cont">'
       		     +'<p class="form-cont-title"><span class="field-required">*</span>数量</p>'  
       		     +'<input type="text" id="count'+j+'" name="count'+j+'" value="'+voData[j].count+'" class="summer-input summer-validatebox" />'    
       		     +'</div>'
       		     ;
        	}  
          	$("#addNew").append(html); 

     	}
     	
    	
    });
    
    
    
    //表单提交
    function submit() {
    	var coupons=[];
    	$('#addNew div').each(function(i){
    		if(i==0){
    			var param1 = $("div input[name='name']").val(); 
        		var param2 = $("div input[name='time']").val();
        		var param3 = $("div input[name='amount']").val();
        		var param4 = $("div input[name='useCond']").val();
        		var param5 = $("div input[name='startTime']").val();
        		var param6 = $("div input[name='endTime']").val();
        		var param8 = $("div input[name='imagePath']").val();
        		var param10 = $("div input[name='count']").val();
        		var param9 = $("div input[name='publicSign']").val();
        		var param11 = $("div input[name='type']").val();
        		var param12 = $("div input[name='id']").val();
    		}else{
    			var param1 = $("div input[name='name"+i+"']").val(); 
        		var param2 = $("div input[name='time"+i+"']").val();
        		var param3 = $("div input[name='amount"+i+"']").val();
        		var param4 = $("div input[name='useCond"+i+"']").val();
        		var param5 = $("div input[name='startTime"+i+"']").val();
        		var param6 = $("div input[name='endTime"+i+"']").val();
        		var param10 = $("div input[name='count"+i+"']").val();
        		var param12 = $("div input[name='id']").val();
    		}
    		
    		if(typeof(param1) == "undefined"){
    			return false;
    		}else{
    			if(i==0){
            		coupons.push(param1+","+param2+","+param3+","+param4+","+param5+","+param6+","+param8+","+param9+","+param10+","+param11+","+param12+"#");
        		}else{
            		coupons.push(param1+","+param2+","+param3+","+param4+","+param5+","+param6+","+param10+","+param12+"#");
        		}
    		}
    		
    		});
		console.log(coupons);

    	/* $("#item").val(coupons);
    	var item = $("#item").val(); */
    	$("#coupons").val(coupons);
    	console.log($("#coupons").val());

    	
         $("#form").form({
        	 buttons : ['#saveBtn'],
             ajax: {
                 url: CONST_URL.EDIT,
                 data:{
     				"coupons" :$("#coupons").val()
     			 }  
             },
             onSubmitSuccess: function (data) {
            	 $.stip({
   	                type:'ok',//显示类型 有info、 ok、 danger
   	                text: '保存成功！',
   	                onBeforeHide: function () {
   	                	backMain();
   	                }
            	 });
              }
         }).form("submit");
    }
    
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_MAIN;
    }
    
    //请求url
    var CONST_URL = {
    	INIT_MAIN: basePath + "${common_url}init",
       	EDIT: basePath + "${common_url}edit"
    };
    
</script>
</body>
</html>